//
//  VideoViewController.m
//  沙哈拉FM
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 S1mpl3. All rights reserved.
//

#import "VideoViewController.h"
#import "FitHeader.h"
#import "CoreDataManager.h"
#import "Focus.h"
#import "S1mpl3RecommendTableViewCell.h"
#import "S1mpl3AlbumViewController.h"
#import "S1mpl3FocusCollectionTableViewCell.h"
#import "S1mpl3HeadInfoViewController.h"
#import "Model.h"
#import "BaseView.h"
#import "TabBarController.h"

@interface VideoViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)UISegmentedControl *segmentedControl;
@property(nonatomic,strong)NSArray *dataSourceArray;
@property(nonatomic,strong)CoreDataManager *manager;
@property(nonatomic,strong)BaseView *segmentView;
@end

@implementation VideoViewController

-(void)viewWillAppear:(BOOL)animated{
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[Model shareInstance].TitleColor, NSForegroundColorAttributeName, nil]];
    [self.navigationController.navigationBar setBarTintColor:[UIColor redColor]];
    _segmentView.backgroundColor = [Model shareInstance].ViewColor;
    _segmentedControl.selectedSegmentIndex = 1;
    _manager = [CoreDataManager shareManager];
    self.view.backgroundColor = [Model shareInstance].ViewColor;
    _tableView.backgroundColor = [Model shareInstance].ViewColor;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //查询
    _dataSourceArray = [NSArray array];
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Focus"];
    _dataSourceArray = [_manager.managedObjectContext executeFetchRequest:fetchRequest error:nil];
  
    
    if (_dataSourceArray.count == 0) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"请添加收藏" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [TabBarController shareInstance].selectedIndex = 0;
            ZBBButton *zbbuton = [[TabBarController shareInstance].myView.subviews firstObject];
            [[TabBarController shareInstance] clickBtn:zbbuton];
//            NSLog(@"%@", [TabBarController shareInstance].myView.subviews);
        }];
        [alertController addAction:action];
        [self presentViewController:alertController animated:YES completion:nil];
    }
    [TabBarController shareInstance].zzzView.backgroundColor = [UIColor whiteColor];
    [_tableView reloadData];
}

-(void)loadView{
    [super loadView];
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 30 * FITHeight, self.view.frame.size.width, self.view.frame.size.height - 64 * FITHeight) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    _segmentView = [[BaseView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30 * FITHeight)];
    _segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"关注", @"收藏"]];
    _segmentedControl.frame = CGRectMake(50 * FITWidth, 0, 275 * FITWidth, 30 * FITHeight);
    _segmentedControl.selectedSegmentIndex = 1;
    [_segmentedControl setTintColor:[UIColor orangeColor]];
    //    [_tableView addSubview:_segmentedControl];
    [_segmentedControl addTarget:self action:@selector(segmentAction) forControlEvents:UIControlEventValueChanged];
    [_segmentView addSubview:_segmentedControl];
    //    _tableView.tableHeaderView = view;
    [self.view addSubview:_segmentView];

}

-(void)segmentAction{
    if (_segmentedControl.selectedSegmentIndex == 0) {
        _dataSourceArray = [NSArray array];
        NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Dic"];
        _dataSourceArray = [_manager.managedObjectContext executeFetchRequest:fetchRequest error:nil];
        
        [_tableView reloadData];

    }else{
        _dataSourceArray = [NSArray array];
        NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Focus"];
        _dataSourceArray = [_manager.managedObjectContext executeFetchRequest:fetchRequest error:nil];
        
        [_tableView reloadData];

    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"定制听";
    self.navigationController.navigationBar.translucent = NO;
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark--tableView代理方法

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_segmentedControl.selectedSegmentIndex == 1) {
        static NSString *identifier = @"cell";
        S1mpl3RecommendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[S1mpl3RecommendTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            
        }
        cell.backgroundColor = [Model shareInstance].ViewColor;
        cell.focus = [_dataSourceArray objectAtIndex:indexPath.row];
        
        return cell;

    }else{
        static NSString *identifier1 = @"cell1";
        S1mpl3FocusCollectionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier1];
        if (!cell) {
            cell = [[S1mpl3FocusCollectionTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier1];
            
        }
        cell.backgroundColor = [Model shareInstance].ViewColor;
//        NSLog(@"%@", [NSKeyedUnarchiver unarchiveObjectWithData:[[_dataSourceArray objectAtIndex:indexPath.row] dic]]);
        cell.dic = [NSKeyedUnarchiver unarchiveObjectWithData:[[_dataSourceArray objectAtIndex:indexPath.row] dic]];
        
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100 * FITHeight;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSourceArray.count;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //    Focus *focus = [_dataSourceArray objectAtIndex:indexPath.row];
//    S1mpl3MoreRecommend *moreRecommend = [[S1mpl3MoreRecommend alloc] init];
//    moreRecommend.albumCoverUrl290 = focus.albumCoverUrl290;
    if (_dataSourceArray.count != 0) {
        if (_segmentedControl.selectedSegmentIndex == 1) {
            S1mpl3AlbumViewController *albumVC = [[S1mpl3AlbumViewController alloc] init];
            if ([[[_dataSourceArray objectAtIndex:indexPath.row] lastUptrackId] length] == 7) {
                albumVC.albumId = [[_dataSourceArray objectAtIndex:indexPath.row] albumId];
            }else{
                
                albumVC.albumId = [[_dataSourceArray objectAtIndex:indexPath.row] iD];
            }
            albumVC.myTitle = [[_dataSourceArray objectAtIndex:indexPath.row] title];
            //    if ([_dataSourceArray[0] isKindOfClass:[S1mpl3MoreRecommend class]]) {
            //        albumVC.moreRecommend = [_dataSourceArray objectAtIndex:indexPath.row];
            //    }else
            albumVC.focus = [_dataSourceArray objectAtIndex:indexPath.row];
            [self.navigationController pushViewController:albumVC animated:YES];
        }else{
            S1mpl3HeadInfoViewController *s1mpl3HeadInfoVC = [[S1mpl3HeadInfoViewController alloc] init];
            s1mpl3HeadInfoVC.s1mpl3HeadDataSourceDic = [NSKeyedUnarchiver unarchiveObjectWithData:[[_dataSourceArray objectAtIndex:indexPath.row] dic]];
            [self.navigationController pushViewController:s1mpl3HeadInfoVC animated:YES];
        }
    }
    
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
